package demo9;


// wait() 以及 notify() : 都是Object方法
// wait(): 分为3个步骤--->首先解开锁-->阻塞等待 --->重新获取锁
public class Test20 {
    public static void main01(String[] args) throws InterruptedException {
        Object object=new Object();
        synchronized(object){
            System.out.println("测试wait");
            object.wait();
            System.out.println("测试完成");
        }
    }


    public static void main(String[] args) {
        Object object=new Object();
        Object object2=new Object();
        Object object3=new Object();
        Object object4=new Object();
        Thread t1=new Thread(()->{
            synchronized(object){
                System.out.println("测试wait1");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("1测试完成");
            }
        });
        Thread t2=new Thread(()->{
            synchronized(object){
                System.out.println("测试wait2");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("2测试完成");
            }
        });Thread t3=new Thread(()->{
            synchronized(object){
                System.out.println("测试wait3");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("3测试完成");
            }
        });Thread t4=new Thread(()->{
            synchronized(object){
                System.out.println("测试wait4");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("4测试完成");
            }
        });
        Thread t5=new Thread(()->{
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            synchronized (object){
                System.out.println("进行通知");
               // object.notify();
                object.notifyAll();
            }
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }
}
